
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ace.dao;

import com.ace.model.bo.Responsable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.ace.model.bo.Utilisateur;
import java.sql.PreparedStatement;

/**
 *
 * @author a.lahideb
 */
public class DaoUser implements IDao<Utilisateur> {

    @Override
    public List<Utilisateur> selectAll() {
        List<Utilisateur> l = new ArrayList<>();
        try {
            Connection cnx = bdd.seConnecter();
            String sql = "select * from utilisateur";
            Statement stat = cnx.createStatement();
            ResultSet res = stat.executeQuery(sql);

            while (res.next()) {
                Utilisateur u = new Utilisateur(res.getInt("id_util"), res.getString("nom"), res.getString("prenom"), res.getString("date_de_naissance"), res.getString("adresse"), res.getInt("code_postal"), res.getString("ville"), res.getString("login"), res.getString("mdp"));
                
                l.add(u);

            }

            bdd.seDeconnecter(cnx);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return l;
    }

    @Override
    public void insert(Utilisateur objet) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void insertFromList(List l) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void delete(Utilisateur objet) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void update(Utilisateur objet) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ResultSet selectRetunRes() {
        ResultSet res = null;
         Connection cnx = null;
        try {
             cnx = bdd.seConnecter();
            String sql = "SELECT `nom`, `prenom`,  `date_de_naissance`, `code_postal`, `ville` FROM `utilisateur`";
            Statement stat = cnx.createStatement();
            res = stat.executeQuery(sql);

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
//            bdd.seDeconnecter(cnx);
        }
        return res;

    }

    @Override
    public Utilisateur selectById(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public Utilisateur authentification(String login , String mdp) {
        Utilisateur user = null;
        try {
            Connection cnx = this.bdd.seConnecter();
            String sql = "SELECT * FROM utilisateur WHERE login = ? AND mdp = ?";
            PreparedStatement ps = cnx.prepareStatement(sql);
            ps.setString(1, login);
            ps.setString(2, mdp);
            ResultSet rs = ps.executeQuery();
            //rs.first();
            while(rs.next()) {
                user = new Utilisateur();
                user.setIdUtil(rs.getInt("id_util"));
                user.setNom(rs.getString("nom"));
                user.setPrenom(rs.getString("prenom"));
                user.setDateDeNaissance(rs.getString("date_de_naissance"));
                user.setLogin(rs.getString("login"));
                user.setMdp(rs.getString("mdp"));
                user.setCodePostal(rs.getInt("code_postal"));
                user.setVille(rs.getString("ville"));
                
            }
            
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }
    
    
     public boolean profil(Utilisateur objet) {
        boolean id = false;
        try {
            String sql = "SELECT id_util FROM responsable WHERE id_util = ?";
            Connection cnx = bdd.seConnecter();
            PreparedStatement ps = cnx.prepareStatement(sql);
            ps.setInt(1, objet.getIdUtil());
            ResultSet rs = ps.executeQuery();
            if(rs.first()) {
                id = true;
            }
        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(DaoUser.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }
    
   
}
